package edu.student.models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.util.DB;

/**
Author: Alpna
**/
public class Semester {
	private int semesterId;
	private String semesterName;
	private char isCurrent;
	
	public Semester(int semesterId, String semesterName, char isCurrent) {
		super();
		this.semesterId = semesterId;
		this.semesterName = semesterName;
		this.isCurrent = isCurrent;
	}
	public int getSemesterId() {
		return semesterId;
	}
	public void setSemesterId(int semesterId) {
		this.semesterId = semesterId;
	}
	public String getSemesterName() {
		return semesterName;
	}
	public void setSemesterName(String semesterName) {
		this.semesterName = semesterName;
	}
	public char getIsCurrent() {
		return isCurrent;
	}
	public void setIsCurrent(char isCurrent) {
		this.isCurrent = isCurrent;
	}
	/*fetch semester name from semester id*/
	public static String getSemesterName(int semesterIdTemp) {
		ResultSet resultSet = null;
		String semesterNameTemp="";
		String query = "select semester_name from semester where semester_id=" + semesterIdTemp+"" ;
		System.out.println("\nINSIDE getSemesterName() of Semester.java class....\n");		
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			if (resultSet.next()){
				semesterNameTemp =resultSet.getString("semester_name");
				/*fetch the value of semester name for the id*/
				
			}
		} catch (SQLException e) {
	       System.out.println("Exception while reading from db"+ e);
		}
		DB.close(connection);
		return semesterNameTemp;
	}
	/*fetch semester name from semester id*/
	public static int getSemesterId(String semesterNameTemp) {
		ResultSet resultSet = null;
		int semesterIdTemp=0;
		String query = "select semester_id from semester where semester_name=" +"'"+ semesterNameTemp+"'" ;
		System.out.println("\nINSIDE getSemesterId() of Semester.java class....\n");	
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			if (resultSet.next()){
				semesterIdTemp =resultSet.getInt("semester_id");
				/*fetch the value of semester name for the id*/
				
			}
		} catch (SQLException e) {
	       System.out.println("Exception while reading from db"+ e);
		}
		DB.close(connection);
		return semesterIdTemp;
	}
	/*fetch semester name from semester id*/
	public static int checkSemesterDurationIsCurrent(String semesterNameTemp) {
		ResultSet resultSet = null;
		int semesterIdTemp=0;
		int display=0;
		String query = "select duration from semester where semester_name=" +"'"+ semesterNameTemp+"' and duration<sysdate()" ;
		System.out.println("\nINSIDE getSemesterId() of Semester.java class....\n");	
		Connection connection = DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			if (resultSet.next()){
				display=1;
				
			}
		} catch (SQLException e) {
	       System.out.println("Exception while reading from db"+ e);
		}
		DB.close(connection);
		return display;
	}
}
